package com.huawei.updatesdk.a.a.c;

import android.text.TextUtils;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.updatesdk.a.a.b.a.a.a;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Locale;

public abstract class e {
    public static String a(String str) {
        String str2 = null;
        if (str != null) {
            try {
                str2 = URLEncoder.encode(str, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("~", "%7E");
            } catch (Throwable e) {
                a.a("StringUtils", "encode2utf8 error", e);
            }
        }
        return str2;
    }

    public static boolean b(String str) {
        return str != null && str.trim().startsWith("{") && str.trim().endsWith("}");
    }

    public static String c(String str) {
        String e = e(str);
        return e == null ? null : e.toLowerCase(Locale.getDefault());
    }

    public static byte[] d(String str) {
        char[] toCharArray = str.toCharArray();
        int length = toCharArray.length / 2;
        byte[] bArr = new byte[length];
        for (int i = 0; i < length; i++) {
            int digit = (Character.digit(toCharArray[i * 2], 16) << 4) | Character.digit(toCharArray[(i * 2) + 1], 16);
            if (digit > 127) {
                digit -= 256;
            }
            bArr[i] = Byte.valueOf(String.valueOf(digit)).byteValue();
        }
        return bArr;
    }

    private static String e(String str) {
        String str2 = null;
        if (!TextUtils.isEmpty(str)) {
            try {
                MessageDigest instance = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256);
                instance.update(str.getBytes("UTF-8"));
                StringBuffer stringBuffer = new StringBuffer();
                for (byte b : instance.digest()) {
                    stringBuffer.append(String.format(Locale.ROOT, "%02X", new Object[]{Byte.valueOf(b)}));
                }
                str2 = stringBuffer.toString();
            } catch (Exception e) {
                a.d("StringUtils", "sha256EncryptStr error:Exception");
            }
        }
        return str2;
    }
}
